package main.java.middle;

/**
 * @Author: 郑国荣
 * @Date: 2023-08-20-10:06
 * @Description:
 */
public class MyAtoi {
    public static int myatoi(String s){
        s=s.trim();
        if (s.isEmpty()) return 0;
        char firstChar = s.charAt(0);
        int start = 0;
        int sign =1;
        long result =0;
        if (firstChar=='+'){
            start++;
        } else if (firstChar == '-') {
            start++;
            sign=-1;
        }
        for (int i = start; i < s.length(); i++) {
            char currentChar = s.charAt(i);
            if (!Character.isDigit(currentChar)) break;
            result = result*10 + (currentChar-'0');
            if (sign==1 && result > Integer.MAX_VALUE) return Integer.MAX_VALUE;
            if (sign==-1 && -result<Integer.MIN_VALUE) return Integer.MIN_VALUE;
        }

        return (int)(result*sign);
    }

    public static void main(String[] args) {
        String s = "  -91283472332";
        System.out.println(myatoi(s));
        System.out.println(Integer.MIN_VALUE);
    }

    }


